package com.twt.config.mybatis.surppot;

import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author: tangwt
 * @date: 2022/5/19 21:51
 * @version:1.0
 **/
@Slf4j
public class DruidDataSourceBuilder extends AbstractDataSourceBuilder {

    @Override
    protected void registryDataSource() {
        if (log.isDebugEnabled()){
            log.info("数据源类型【{}】","druid");
        }
        Map<String, Object> newMap = new HashMap<>(16);
        newMap.put("url", dataSourceProperty.getUrl());
        newMap.put("username", dataSourceProperty.getUsername());
        newMap.put("password", dataSourceProperty.getPassword());
        newMap.put("driverClassName", dataSourceProperty.getDriverClassName());
        Map<String, Object> stringObjectMap = Optional.ofNullable(dataSourceProperty.getOtherParams()).orElse(Maps.newHashMap());
        newMap.putAll(stringObjectMap);
        this.registerBean(beanName, newMap, this.getDataSourceClass());
    }

    @Override
    public Class getDataSourceClass() {
        return DruidDataSource.class;
    }
}
